import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { Park } from './park';

/*
  Generated class for the ParkDataProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class ParkDataProvider {
  private readonly baseUser: string = 'https://api.bmob.cn/1';
  private readonly httpHeaders:  HttpHeaders = new HttpHeaders({
    'X-Bmob-Application-Id':'1d5ed51a1097938c4392bc8b1454d9ca',
    'X-Bmob-REST-API-Key':'37604b14b7c597853a15fda51681177e',
    'Content-Type':'application/json'
  });
  constructor(public http: HttpClient) {
  }

  /**
   * 分页获取景区数据
   * @param num 
   * @param page 
   */
  getParks(num: number,page: number): Observable<Park[]>{
    let _params = new HttpParams()
    .set('limit', `${num}`)
    .set('skip', `${(page - 1)* num}`);
    return this.http.get<Park[]>(`${this.baseUser}/classes/parks`,
    {headers: this.httpHeaders, params: _params}).map(_=>_['results']);
  }

}
